package knowse.app;

import java.util.ArrayList;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.util.Log;

import knowse.app.daos.DBHelper;

public class DataModel {

	public DataModel() {
	}

	public HeatMapData getHeatMap(MapDataType mapDataType) {
		
		//Get data depending on MapDataType arg.
		switch (mapDataType) {
			case TEST_PER_PROJECT:
				return queryHeatMap("SELECT ProjectName, SUM(NumTests),StartDate FROM TestStatus GROUP BY StartDate");
			case TESTFAILS_PER_PROJECT:
				return queryHeatMap("SELECT ProjectName, SUM(NumFailures),StartDate FROM TestStatus GROUP BY StartDate");
			case TESTERRORS_PER_PROJECT:
				return queryHeatMap("SELECT ProjectName, SUM(NumErrors),StartDate FROM TestStatus GROUP BY StartDate");
			case COMMIT_PER_USER:
				return queryHeatMap("SELECT COUNT(*) as 'Number of commits', AuthorName, StartDate FROM LogCommit GROUP BY AuthorName,StartDate ORDER BY StartDate");
			default:
	            Log.w("KnowSE", "MapDataType not found!");
	            break;
		}
		
		return new HeatMapData();
	}
	
	public HeatMapData queryHeatMap (String query)
	{
		//Opens db connection for reading
		SQLiteDatabase db = new DBHelper().getReadableDatabase();
		String sqlQuery;
		Cursor cursor;
		HeatMapData heatMapData = new HeatMapData();
		
		sqlQuery = query;
		cursor = db.rawQuery(sqlQuery, null);
		heatMapData = new HeatMapData();
		
		cursor.moveToFirst();
		//The first argument of addRow is the unique identifier that will be used when grouped
		while (!cursor.isAfterLast()){
			heatMapData.addRow(cursor.getString(0), (float)cursor.getInt(1));
			cursor.moveToNext();
		}
		return heatMapData;
	}
}
